package org.six11.olive3.picturephone.server.handler;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.six11.olive3.net.Message;
import org.six11.olive3.net.MessageHandler;
import org.six11.olive3.net.MessageNames;
import org.six11.util2.Debug;

import sun.misc.resources.Messages;

/**
 * 
 * 
 * @author Gabe Johnson <johnsogg@cmu.edu>
 */
public class MessageCacheHandler extends MessageHandler {

  Map<String, List<Message>> messages;

  public MessageCacheHandler() {
    messages = new HashMap<String, List<Message>>();
  }

  public void handleLater(Message m) {
    String k = m.getParamValue(MessageNames.PARAM_USER_ID);
    if (k == null)
      return;
    if (!messages.containsKey(k)) {
      messages.put(k, new ArrayList<Message>());
    }
    messages.get(k).add(m);
  }

  public List<Message> getMessages(String userID) {
    bug("Are you sure you want getMessages and not popMessages ?");
    return messages.get(userID);
  }
  
  public List<Message> popMessages(String userID) {
    return messages.remove(userID);
  }

}

// http://www.bbqaddicts.com/blog/recipes/bacon-explosion/